package com.github.prontera.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.domain.Client;
import com.github.prontera.domain.ClientFollow;
import com.github.prontera.domain.enums.ClientFollowStatusEnum;
import com.github.prontera.persistence.ClientFollowMapper;
import com.github.prontera.persistence.CrudMapper;
import com.github.prontera.util.Page;
import com.google.common.base.Preconditions;

/**
 * 关注
 */
@Service
public class ClientFollowService extends CrudServiceImpl<ClientFollow>{
    
	@Autowired
	private ClientFollowMapper mapper;
	@Autowired
	private ClientService clientService;
	
  	@Autowired
    public ClientFollowService(CrudMapper<ClientFollow> mapper) {
        super(mapper);
    }

  	public	ClientFollow getFollow(ClientFollow clientFollow) {
  		return mapper.getFollow(clientFollow);
  	}
  	
  	/**
  	 * 添加关注
  	 */
  	@Transactional(propagation = Propagation.REQUIRED)
  	public boolean insertFollow(ClientFollow clientFollow) {
  		Preconditions.checkNotNull(clientFollow.getClientId(), "persisting clientId should not be NULL");
  		Preconditions.checkNotNull(clientFollow.getFollowId(), "persisting followId should not be NULL");
  		//之前已经被关注过
  		ClientFollow findFollow=mapper.getFollow(clientFollow);
  		if(findFollow!=null){
  			//已经关注过,但是被再次关注(两次点击关注)
  			if(findFollow.getClientId().longValue()==clientFollow.getClientId().longValue()){
  			//已经被双向关注过(双方互为关注)
  	  			if(findFollow.getStatus()==ClientFollowStatusEnum.TWO_WAY_CONCERN){
  	  				return true;
  	  			}
  	  			Client client=new Client();
				client.setFollowCount(1);
				client.setId(clientFollow.getClientId());
				clientService.updateCount(client);
				
				client=new Client();
				client.setFansCount(1);
				client.setId(clientFollow.getFollowId());
				clientService.updateCount(client);
				
				findFollow.setStatus(ClientFollowStatusEnum.TWO_WAY_CONCERN);
				mapper.update(findFollow);
  			}
  			//以前是clientid方主动关注
  			if(findFollow.getFollowId().longValue()==clientFollow.getClientId().longValue()){
  				findFollow.setStatus(ClientFollowStatusEnum.TWO_WAY_CONCERN);
  				
  				Client client=new Client();
  				client.setFollowCount(1);
  				client.setId(clientFollow.getClientId());
  				clientService.updateCount(client);
  				
  				client=new Client();
  				client.setFansCount(1);
  				client.setId(clientFollow.getFollowId());
  				clientService.updateCount(client);
  				
  				mapper.update(findFollow);
  				return false;
  			}
  		}else{
  			clientFollow.setStatus(ClientFollowStatusEnum.ACTIVE_CONCERN);
  			
  			Client client=new Client();
			client.setFollowCount(1);
			client.setId(clientFollow.getClientId());
			clientService.updateCount(client);
			
			client=new Client();
			client.setFansCount(1);
			client.setId(clientFollow.getFollowId());
			clientService.updateCount(client);
  			
  			return mapper.insert(clientFollow)>0;
  		}
  		return false;
	}
  	
  	/**
  	 * 取消关注
  	 */
  	@Transactional(propagation = Propagation.REQUIRED)
 	public boolean removeFollow(ClientFollow clientFollow) {
 		Preconditions.checkNotNull(clientFollow.getClientId(), "persisting clientId should not be NULL");
  		Preconditions.checkNotNull(clientFollow.getFollowId(), "persisting followId should not be NULL");
  		//之前已经被关注过
  		ClientFollow findFollow=mapper.getFollow(clientFollow);
  		if(findFollow!=null){
  			//已经关注过,且当前clientid是主动关注方
  			if(findFollow.getClientId().longValue()==clientFollow.getClientId().longValue()){
  				System.out.println("-----------------1------------");
  				Client client=new Client();
  				client.setFansCount(-1);
  				client.setId(findFollow.getFollowId());
  				clientService.updateCount(client);
  				
  				client=new Client();
  				client.setFollowCount(-1);
  				client.setId(findFollow.getClientId());
  				clientService.updateCount(client);
  				//已经被双向关注过(双方互为关注)
  	  			if(findFollow.getStatus()==ClientFollowStatusEnum.TWO_WAY_CONCERN){
  	  				findFollow.setStatus(ClientFollowStatusEnum.PASSIVE_CONCERN);
  	  			System.out.println("-----------------2------------");
  	  				update(findFollow);
  	  				return true;
  	  			//首次关注主动方取消关注
  	  			}else if (findFollow.getStatus()==ClientFollowStatusEnum.ACTIVE_CONCERN){
  	  				System.out.println("-----------------3------------");
  	  				return mapper.removeFollow(clientFollow)>0;
				}
  	  		//已经关注过,且当前clientid是被动关注方
  			}else if (findFollow.getFollowId().longValue()==clientFollow.getClientId().longValue()) {
  				System.out.println("-----------------4------------");
  				Client client=new Client();
  				client.setFollowCount(-1);
  				client.setId(findFollow.getFollowId());
  				clientService.updateCount(client);
  				
  				client=new Client();
  				client.setFansCount(-1);
  				client.setId(findFollow.getClientId());
  				clientService.updateCount(client);
  				//已经被双向关注过(双方互为关注)
  	  			if(findFollow.getStatus()==ClientFollowStatusEnum.TWO_WAY_CONCERN){
  	  			findFollow.setStatus(ClientFollowStatusEnum.ACTIVE_CONCERN);
  	  				System.out.println("-----------------5------------");
  	  				update(findFollow);
  	  				return true;
  	  			//首次关注主动方取消关注
  	  			}else if (findFollow.getStatus()==ClientFollowStatusEnum.PASSIVE_CONCERN){
  	  				System.out.println("-----------------6------------");
  	  				return mapper.removeFollow(clientFollow)>0;
				}
			}
  		}else{
  			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"请确认双方关系!");
  		}
  		return true;
	}
 	
 	/**
  	 * 用户关注的
  	 */
	public List<ClientFollow> findClientFollow(ClientFollow entity) {
		return mapper.clientFollow(entity);
	}
  	
	/**
  	 * 用户的粉丝
  	 */
	public List<ClientFollow> findClientFans(ClientFollow entity) {
		return mapper.clientFans(entity);
	}
  	
  	/**
  	 * 用户关注的
  	 */
	public Page<ClientFollow> clientFollow(ClientFollow entity) {
		long count = mapper.count(entity);
		Page<ClientFollow> page = new Page<ClientFollow>(entity.getPageIndex(), entity.getPageSize());
		page.setData(mapper.clientFollow(entity));
		page.setTotalCount(count);
		return page;
	}
  	
	/**
  	 * 用户的粉丝
  	 */
	public Page<ClientFollow> clientFans(ClientFollow entity) {
		long count = mapper.count(entity);
		Page<ClientFollow> page = new Page<ClientFollow>(entity.getPageIndex(), entity.getPageSize());
		page.setData(mapper.clientFans(entity));
		page.setTotalCount(count);
		return page;
	}

}
